from PyQt5 import QtGui
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QCheckBox


class TableCheckBox(QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.checkbox = QCheckBox()
        layout = QVBoxLayout()
        self.setLayout(layout)
        layout.addWidget(self.checkbox)
        layout.setAlignment(Qt.AlignCenter)

        # self.checkbox.stateChanged.connect(lambda: print(self.sender()))

    def state(self) -> bool:
        """
        True, если чекбокс выбран, иначе False
        @return: выбран ли чекбокс
        """
        return self.checkbox.isChecked()

    def set_state(self, state):
        self.checkbox.setCheckState(state)

    def mousePressEvent(self, a0: QtGui.QMouseEvent) -> None:
        if self.checkbox.isChecked():
            self.checkbox.setCheckState(0)
        else:
            self.checkbox.setCheckState(2)


def get_name_with_next_number(current_name):
    cnt = ''
    right = len(current_name) - 1
    while current_name[right].isdigit():
        cnt = current_name[right] + cnt
        right -= 1
    if cnt == '':
        cnt = 1
    else:
        cnt = int(cnt) + 1
    return current_name[:right + 1] + str(cnt)
